package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.http.listener.HttpCallback;
import com.lxj.xpopup.XPopup;
import com.qiantu.api.db.LLSceneDBManager;
import com.qiantu.api.entity.CreateSceneResult;
import com.qiantu.api.entity.SceneBean;
import com.qiantu.api.entity.SceneInterruptModeBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.event.RefreshSceneEvent;
import com.qiantu.phone.event.RefreshSceneIntentEvent;
import com.qiantu.phone.event.UpdateFavouriteEvent;
import com.qiantu.phone.manager.ActivityManager;
import com.qiantu.phone.ui.adapter.InterruptModeAdapter;
import com.qiantu.phone.ui.adapter.SceneDetailAdapter;
import com.qiantu.phone.ui.dialog.BottomInterruptSceneDialog;
import com.qiantu.phone.utils.TempDataHolder;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class SceneInterruptModeActivity extends AppActivity {
    public String sceneSerialNo;
    private  SceneInterruptModeBean sceneInterruptModeBean;
    protected RecyclerView mRecyclerView;
    protected InterruptModeAdapter interruptModeAdapter;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_scene_interrup_mode;
    }

    @Override
    protected void initView() {
        sceneSerialNo = getString("sceneSerialNo");
        mRecyclerView = findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    @Override
    protected void initData() {
        LLHttpManager.getInterrupData(this, sceneSerialNo, new HttpCallback<HttpData<SceneInterruptModeBean>>(this){
            @Override
            public void onSucceed(HttpData<SceneInterruptModeBean> result) {
                super.onSucceed(result);
                sceneInterruptModeBean = result.getData();
                setData();
            }
        });
    }

    @Override
    public void onRightClick(View view) {
        LLHttpManager.setInterrupData(this, sceneInterruptModeBean, new HttpCallback<HttpData<Void>>(this){
            @Override
            public void onSucceed(HttpData<Void> result) {
                super.onSucceed(result);
                finish();
            }
        });
    }

    private void setData() {
        List<SceneInterruptModeBean.InterruptSceneBean> rules = new ArrayList<>();
        for(SceneInterruptModeBean.InterruptSceneBean bean : sceneInterruptModeBean.getRules()){
            SceneBean sceneBean = LLSceneDBManager.getInstance(this).getSceneBySerialNo(bean.getSceneSerialNo());
            SceneBean subSceneBean = LLSceneDBManager.getInstance(this).getSceneBySerialNo(bean.getSubSceneSerialNo());
            if(subSceneBean != null && sceneBean != null){
                bean.setSceneName(sceneBean.getName());
                bean.setSubSceneName(subSceneBean.getName());
                rules.add(bean);
            }
        }
        sceneInterruptModeBean.setRules(rules);
        interruptModeAdapter = new InterruptModeAdapter(this, sceneInterruptModeBean);
        mRecyclerView.setAdapter(interruptModeAdapter);
    }
}
